<template>
	<view>
		<u-navbar title="个人资料" :autoBack="true" :placeholder="true"></u-navbar>

		<view class="picture">
			<image :src="userinfo.avatar" mode="" class="avatar" @click="pickerImg"></image>
		</view>
		<view class="from">
			<view class="name item">
				<text style="color: rgba(0, 0, 0, 0.4);">呢称</text>
				<input class="nickname" type="nickname" placeholder="请输入昵称" v-model="userinfo.nickname" />
			</view>

			<view class="signature item">
				<text style="color: rgba(0, 0, 0, 0.4);">性别</text>
				<view class="">
					<radio-group @change="radioChange" class="radio-group">
						<label class="weui-cell weui-check__label" v-for="item in items" key="index">
							<view class="weui-cell__hd">
								<radio :value="item.value" color="#000" class="sex" :checked="userinfo.gender == item.value" />
							</view>
							<view class="weui-cell__bd">{{item.name}}</view>
						</label>
					</radio-group>
				</view>
			</view>

			<view class="signature item">
				<text style="color: rgba(0, 0, 0, 0.4);">手机</text>
				<input type="text" class="phone" placeholder="用户手机号" v-model="userinfo.phone" @input="mobileChange">
			</view>
		</view>
		<view class="btn" @click="btnClick">
			保存
		</view>

		<view class="vision">v-{{version}}</view>
	</view>
</template>

<script>
	import {
		BaseURL
	} from '@/utils/global.js'
	export default {
		data() {
			return {
				userinfo: {}, //用户信息
				arr: [{
					url: "",
				}],
				version: '1.0.0', //版本
				items: [{
					value: '1',
					name: '男'
				}, {
					value: '0',
					name: '女'
				}]
			};
		},
		onLoad() {
			this.getuserinfo()
		},
		onShow() {
			const miniProgram = wx.getAccountInfoSync();
			this.version = miniProgram.miniProgram.version || '1.0.0';
		},
		methods: {
			// 保存
			async btnClick() {
				console.log(this.userinfo);
				let userInfo = {
					uid: wx.getStorageSync('userInfo').uid,
					nickname: this.userinfo.nickname || '',
					avatar: this.userinfo.avatar || '',
					gender: this.userinfo.gender || '',
					phone: this.userinfo.phone || '',
				}

				let res = await this.$api.setuserinfo(userInfo)
				this.$u.toast('修改成功')
				if (res.code === 0) {
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				}
			},
			// 获取个人信息
			async getuserinfo() {
				let res = await this.$api.getuserinfo({
					uid: wx.getStorageSync('userInfo').uid
				})
				this.userinfo = res.data
				this.arr[0].url = res.data.avatar
			},

			// 性别切换
			radioChange(e) {
				console.log(e);
				this.userinfo.gender = e.detail.value
			},

			// 手机号切换
			mobileChange(e) {
				this.userinfo.mobile = e.detail.value
			},
			// 选择图片
			pickerImg(e) {
				let _this = this;
				console.log('；；；');

				wx.chooseMedia({
					count: 9,
					mediaType: ['image', 'video'],
					sourceType: ['album', 'camera'],
					maxDuration: 30,
					camera: 'back',
					success(res) {
						console.log(res.tempFiles[0].tempFilePath)
						console.log(res.tempFiles[0].size)
					},
					fail(err) {
						console.log(err);
					}
				})
			},
			// 上传图片
			uploadFilePromise(url) {
				let _this = this;
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: BaseURL + '/api/common/upload',
						filePath: url,
						name: 'file',
						formData: {
							'type': 'image'
						},
						success: (res) => {
							_this.userinfo.avatar = JSON.parse(res.data).data.fullurl;

							wx.$toast('点击保存按钮后完成修改')
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.picture {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		height: 480rpx;
		border-radius: 100rpx;
	}

	.from {
		box-sizing: border-box;
		padding: 0 32rpx;
		color: rgba(0, 0, 0, 0.8);
		font-size: 28rpx;
		font-weight: 400;



		.item {
			height: 104rpx;
			background-color: white;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 20rpx;
			padding: 0 20rpx;
			border-radius: 10rpx;

		}
	}

	.btn {
		margin: 0rpx auto;
		width: 424rpx;
		height: 88rpx;
		line-height: 88rpx;
		text-align: center;
		border-radius: 600rpx;
		opacity: 1;
		background: $boxBg;
		color: #FFFFFF;
		position: absolute;
		bottom: 100rpx;
		left: calc(50% - 212rpx);
	}

	.vision {
		position: absolute;
		bottom: 40rpx;
		right: 40rpx;
		color: #ddd;
		font-size: 22rpx;
	}

	.weui-cell {
		display: flex;
	}

	.radio-group {
		display: flex;
	}

	.sex {
		margin-left: 10rpx;
	}

	.avatar {
		width: 200rpx;
		height: 200rpx;
		border-radius: 100rpx;
	}

	.nickname {
		flex: 1;
		margin-left: 32rpx;
		text-align: right;
	}

	.phone {
		flex: 1;
		margin-left: 32rpx;
		text-align: right;
	}
</style>